package com.ithuameng.admin.app.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithuameng.admin.app.system.model.SequenceRule;
import com.ithuameng.admin.app.system.model.admin.qo.SequenceRuleQo;
import com.ithuameng.admin.app.system.model.admin.vo.SequenceRuleUpdateVo;
import com.ithuameng.admin.app.system.model.vo.AdminPageResVo;
import com.ithuameng.admin.app.system.model.vo.R;
import com.ithuameng.admin.app.system.service.ISequenceRuleService;
import com.ithuameng.admin.enums.CommonError;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 流水号规则 控制器
 *
 * @author ithuameng
 */
@Api(tags = "流水号控制器")
@RestController
@RequestMapping(value = "/admin/v1/sequence")
public class SequenceRuleController extends AdminBaseController {

    @Resource
    private ISequenceRuleService sequenceRuleService;

    @ApiOperation("获取流水号分页列表")
    @PreAuthorize("@ss.hasPermi('system:sequence:list')")
    @GetMapping(value = "/list")
    public R<AdminPageResVo<SequenceRule>> list(SequenceRuleQo sequenceRuleQo) {
        Page<SequenceRule> page = new Page<>(sequenceRuleQo.getPageNumber(), sequenceRuleQo.getPageSize());
        page.addOrder(OrderItem.desc(SequenceRule.UPDATE_TIME));
        IPage<SequenceRule> pageList = sequenceRuleService.listSequenceRule(page, sequenceRuleQo);
        AdminPageResVo<SequenceRule> sequenceRulePage = new AdminPageResVo<>();
        sequenceRulePage.setRecords(pageList.getRecords());
        sequenceRulePage.setPage(pageList.getPages());
        sequenceRulePage.setTotal(pageList.getTotal());
        return new R<>(sequenceRulePage);
    }

    @ApiOperation("修改流水号")
    @PreAuthorize("@ss.hasPermi('system:sequence:edit')")
    @PostMapping(value = "/edit")
    public R<Boolean> edit(@Valid @RequestBody SequenceRuleUpdateVo sequenceRuleUpdateVo) {
    	if (sequenceRuleUpdateVo.getType()== null) {
			return new R<>(CommonError.BIZ_ERR, "类型不正确");
		}
        SequenceRule sequenceRuleDO = new SequenceRule();
        BeanUtils.copyProperties(sequenceRuleUpdateVo, sequenceRuleDO);
        sequenceRuleService.updateById(sequenceRuleDO);
        return new R<>(CommonError.OK, "修改成功");
    }
}
